package com.food.restaurantpro.database.tables;

import java.util.ArrayList;
import java.util.List;

import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.database.RestaurantAppContentProvider;
import com.food.restaurantpro.objects.Reservation;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public class ReservationTable {
	public static final String TAG = "ReservationTable";
	
	public static final String TABLE_NAME = "reservation";
	
	public static final String RESERVATION_ID = "reservationID";
	public static final String RESERVATION_DATE = "reservationDate";
	public static final String RESERVATION_TIME = "reservationTime";
	public static final String RESERVATION_NUMBER = "reservationNumber";
	public static final String RESERVATION_MEMBER_CODE = "reservationMemberCode";
	public static final String RESERVATION_STATUS = "reservationStatus";

	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ RESERVATION_ID + " text primary key not null, "
			+ RESERVATION_DATE + " text not null, "
			+ RESERVATION_TIME + " text not null, "
			+ RESERVATION_NUMBER + " text not null, "
			+ RESERVATION_MEMBER_CODE + " text not null, "
			+ RESERVATION_STATUS + " text not null);";
	
	public static void deleteAll()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, null, null);
	}
	/**
	 * @param listReservations list reservation to insert to db
	 * @return the number of newly created rows. 
	 */
	public static int insertListReservations(List<Reservation> listReservations) {
		if(listReservations != null && listReservations.size() > 0) {
			int size = listReservations.size();
			//show progress
			ContentValues[] allValues = new ContentValues[size];
			for (int i = 0; i < size; i++) {
				ContentValues value = new ContentValues();
				Reservation reservation = listReservations.get(i);
				value.put(RESERVATION_ID, reservation.getReservationId());
				value.put(RESERVATION_DATE, reservation.getReservationDate());
				value.put(RESERVATION_TIME, reservation.getReservationTime());
				value.put(RESERVATION_NUMBER, reservation.getReservationNumber());
				value.put(RESERVATION_MEMBER_CODE, reservation.getReservationMemberCode());
				value.put(RESERVATION_STATUS, reservation.getReservationStatus());
				
				allValues[i] = value;
			}
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowadd = resolver.bulkInsert(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, allValues);
			return rowadd;
			//clode progress
		}
		return 0;
	}
	
	public static ArrayList<Reservation> getAllReservations()
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Reservation> arrayList = new ArrayList<Reservation>();
		Cursor cursor = resolver.query(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, null, null, null, null);
		if(cursor != null){
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Reservation reservation = new Reservation();
						
						String reservationId = cursor.getString(cursor.getColumnIndex(RESERVATION_ID));
						reservation.setReservationId(reservationId);
						
						String reservationDate = cursor.getString(cursor.getColumnIndex(RESERVATION_DATE));
						reservation.setReservationDate(reservationDate);
						
						String reservationTime = cursor.getString(cursor.getColumnIndex(RESERVATION_TIME));
						reservation.setReservationTime(reservationTime);
						
						String reservationNumber = cursor.getString(cursor.getColumnIndex(RESERVATION_NUMBER));
						reservation.setReservationNumber(reservationNumber);
						
						String reservationMemberCode = cursor.getString(cursor.getColumnIndex(RESERVATION_MEMBER_CODE));
						reservation.setReservationMemberCode(reservationMemberCode);
						
						String reservationStatus = cursor.getString(cursor.getColumnIndex(RESERVATION_STATUS));
						reservation.setReservationStatus(reservationStatus);
						
						arrayList.add(reservation);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllReservations()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static ArrayList<Reservation> getAllReservationsByMember(String MemberCode)
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Reservation> arrayList = new ArrayList<Reservation>();
		String fillter = RESERVATION_MEMBER_CODE + "='" + MemberCode + "'";
		Cursor cursor = resolver.query(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, null, fillter, null, null);
		if(cursor != null){
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Reservation reservation = new Reservation();
						
						String reservationId = cursor.getString(cursor.getColumnIndex(RESERVATION_ID));
						reservation.setReservationId(reservationId);
						
						String reservationDate = cursor.getString(cursor.getColumnIndex(RESERVATION_DATE));
						reservation.setReservationDate(reservationDate);
						
						String reservationTime = cursor.getString(cursor.getColumnIndex(RESERVATION_TIME));
						reservation.setReservationTime(reservationTime);
						
						String reservationNumber = cursor.getString(cursor.getColumnIndex(RESERVATION_NUMBER));
						reservation.setReservationNumber(reservationNumber);
						
						String reservationMemberCode = cursor.getString(cursor.getColumnIndex(RESERVATION_MEMBER_CODE));
						reservation.setReservationMemberCode(reservationMemberCode);
						
						String reservationStatus = cursor.getString(cursor.getColumnIndex(RESERVATION_STATUS));
						reservation.setReservationStatus(reservationStatus);
						
						arrayList.add(reservation);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllReservations()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static ArrayList<Reservation> getAllReservationsWithStatus(String Status)
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Reservation> arrayList = new ArrayList<Reservation>();
		String fillter = RESERVATION_STATUS + "=" + Status;
		Cursor cursor = resolver.query(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, null, fillter, null, null);
		if(cursor != null){
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Reservation reservation = new Reservation();
						
						String reservationId = cursor.getString(cursor.getColumnIndex(RESERVATION_ID));
						reservation.setReservationId(reservationId);
						
						String reservationDate = cursor.getString(cursor.getColumnIndex(RESERVATION_DATE));
						reservation.setReservationDate(reservationDate);
						
						String reservationTime = cursor.getString(cursor.getColumnIndex(RESERVATION_TIME));
						reservation.setReservationTime(reservationTime);
						
						String reservationNumber = cursor.getString(cursor.getColumnIndex(RESERVATION_NUMBER));
						reservation.setReservationNumber(reservationNumber);
						
						String reservationMemberCode = cursor.getString(cursor.getColumnIndex(RESERVATION_MEMBER_CODE));
						reservation.setReservationMemberCode(reservationMemberCode);
						
						String reservationStatus = cursor.getString(cursor.getColumnIndex(RESERVATION_STATUS));
						reservation.setReservationStatus(reservationStatus);
						
						arrayList.add(reservation);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllReservations()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static ArrayList<Reservation> getAllReservationsByMemberWithStatus(String MemberCode, String Status)
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		ArrayList<Reservation> arrayList = new ArrayList<Reservation>();
		String fillter = RESERVATION_MEMBER_CODE + "='" + MemberCode + "' and " + RESERVATION_STATUS + "=" + Status;
		Cursor cursor = resolver.query(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, null, fillter, null, null);
		if(cursor != null){
			if(cursor.getCount()>0){
				try {
					while (!cursor.isLast()) {
						cursor.moveToNext();
						Reservation reservation = new Reservation();
						
						String reservationId = cursor.getString(cursor.getColumnIndex(RESERVATION_ID));
						reservation.setReservationId(reservationId);
						
						String reservationDate = cursor.getString(cursor.getColumnIndex(RESERVATION_DATE));
						reservation.setReservationDate(reservationDate);
						
						String reservationTime = cursor.getString(cursor.getColumnIndex(RESERVATION_TIME));
						reservation.setReservationTime(reservationTime);
						
						String reservationNumber = cursor.getString(cursor.getColumnIndex(RESERVATION_NUMBER));
						reservation.setReservationNumber(reservationNumber);
						
						String reservationMemberCode = cursor.getString(cursor.getColumnIndex(RESERVATION_MEMBER_CODE));
						reservation.setReservationMemberCode(reservationMemberCode);
						
						String reservationStatus = cursor.getString(cursor.getColumnIndex(RESERVATION_STATUS));
						reservation.setReservationStatus(reservationStatus);
						
						arrayList.add(reservation);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Log.e(TAG, "ERROR getAllReservations()", e);
				}
			}
			cursor.close();
		}
		return arrayList;
	}
	
	public static void insertReservation(Reservation reservation) {
		if(reservation != null) {
			ContentValues value = new ContentValues();
			value.put(RESERVATION_ID, reservation.getReservationId());
			value.put(RESERVATION_DATE, reservation.getReservationDate());
			value.put(RESERVATION_TIME, reservation.getReservationTime());
			value.put(RESERVATION_NUMBER, reservation.getReservationNumber());
			value.put(RESERVATION_MEMBER_CODE, reservation.getReservationMemberCode());
			value.put(RESERVATION_STATUS, reservation.getReservationStatus());
				
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			resolver.insert(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, value);
		}
	}
	
	public static int updateReservationByID(Reservation reservation) {
		if(reservation != null) {
			ContentValues value = new ContentValues();
			value.put(RESERVATION_ID, reservation.getReservationId());
			value.put(RESERVATION_DATE, reservation.getReservationDate());
			value.put(RESERVATION_TIME, reservation.getReservationTime());
			value.put(RESERVATION_NUMBER, reservation.getReservationNumber());
			value.put(RESERVATION_MEMBER_CODE, reservation.getReservationMemberCode());
			value.put(RESERVATION_STATUS, reservation.getReservationStatus());
				
			ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
			int rowUpdate = resolver.update(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, value, RESERVATION_ID + "=?", new String[] {reservation.getReservationId()});
			return rowUpdate;
		}
		return -1;
	}
	
	public static void deleteReservationByID(String reservationId)
	{
		ContentResolver resolver = RestaurantApp.CURRENT_CONTEXT.getContentResolver();
		resolver.delete(RestaurantAppContentProvider.RESERVATION_CONTENT_URI, RESERVATION_ID + "=?", new String[] {reservationId});
	}
}
